﻿using Avalonia.Media.Imaging;
using MyTodo.Common;
using MyTodo.Extensions;
using MyTodo.Services;
using MyTodo.Shared.Dtos;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTodo.ViewModels
{
    public class LoginViewModel : BindableBase
    {
        public LoginViewModel(ILoginService service, IEventAggregator aggregator)
        {
            userDto = new RegisterDto();
            ExecuteCommand = new DelegateCommand<string>(Execute);
            this.service = service;
            this.aggregator = aggregator;
        }
        #region 属性
        private readonly IEventAggregator aggregator;


        public event Action<IDialogResult> RequestClose;
        public DelegateCommand<string> ExecuteCommand { get; private set; }

        private string account;

        public string Account
        {
            get { return account; }
            set { account = value; }
        }
        private string password;
        private readonly ILoginService service;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); }
        }

        private RegisterDto userDto;

        public RegisterDto UserDto
        {
            get { return userDto; }
            set { userDto = value; RaisePropertyChanged(); }
        }


        public Bitmap? ImageLogin { get; } = ImageHelper.LoadFromResource(new Uri("avares://MyTodo/Images/login.png"));
        public Task<Bitmap?> ImageFromWebsite { get; } = ImageHelper.LoadFromWeb(new Uri("https://upload.wikimedia.org/wikipedia/commons/4/41/NewtonsPrincipia.jpg"));


        #endregion

        #region Dialog相关方法和属性

        public string Title { get; set; } = "ToDo";
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            Logout();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }

        #endregion
        void Execute(string arg)
        {
            switch (arg)
            {
                case "Login": Login(); break;
                case "Logout": Logout(); break;
                // 跳转注册页面
                case "Go": SelectedIndex = 1; break;
                // 跳转登陆页面
                case "Return": SelectedIndex = 0; break;
                case "Register": Register(); break;


            }
        }

        private async void Register()
        {
            if (string.IsNullOrWhiteSpace(UserDto.UserName) ||
                string.IsNullOrWhiteSpace(UserDto.Account) ||
                string.IsNullOrWhiteSpace(UserDto.Password))
            {
                aggregator.SendMessage("不能为空", "Login");
                return;
            }

            if (UserDto.Password != UserDto.NewPassword)
            {
                aggregator.SendMessage("两次密码不一致", "Login");
                return;
            }

            var registerResult = await service.RegisterAsync(new UserDto()
            {
                Account = UserDto.Account,
                UserName = UserDto.UserName,
                Password = UserDto.Password
            });

            if (registerResult != null && registerResult.Status)
            {
                aggregator.SendMessage("注册成功", "Login");
                SelectedIndex = 0;
                return;
            }

            aggregator.SendMessage(registerResult.Message, "Login");
        }

        private void Logout()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }

        private async void Login()
        {
            if (string.IsNullOrWhiteSpace(Account) ||
                string.IsNullOrWhiteSpace(Password))
                return;

            var loginResult = await service.LoginAsync(new UserDto()
            {
                Account = Account,
                Password = Password
            });

            if (loginResult.Status)
            {
                AppSession.UserName = loginResult.Result.UserName;
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                return;
            }

            aggregator.SendMessage(loginResult.Message, "Login");
        }
    }
}
